package com.postek.coyote.postekwifi;

import java.util.ArrayList;

import com.postek.cdf.CDFPTKAndroid;
import com.postek.cdf.CDFPTKAndroidImpl;
import com.postek.coyote.postekwifi.bean.BBean;
import com.postek.coyote.postekwifi.bean.DMBean;
import com.postek.coyote.postekwifi.bean.HXBean;
import com.postek.coyote.postekwifi.bean.LEBean;
import com.postek.coyote.postekwifi.bean.LOBean;
import com.postek.coyote.postekwifi.bean.LSBean;
import com.postek.coyote.postekwifi.bean.LWBean;
import com.postek.coyote.postekwifi.bean.LabelBean;
import com.postek.coyote.postekwifi.bean.MXBean;
import com.postek.coyote.postekwifi.bean.MessageBean;
import com.postek.coyote.postekwifi.bean.PDFBean;
import com.postek.coyote.postekwifi.bean.QRBean;
import com.postek.coyote.postekwifi.bean.TBean;
import com.postek.coyote.postekwifi.bean.XBean;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener {

    private static final int REQUEST_CODE = 10000;
    private static final int RESULT_CODE = 11111;
    public static CDFPTKAndroid cdf = null;
    public static MessageBean messageBean = new MessageBean();
    public static ArrayList<LabelBean> labelBeans = new ArrayList<LabelBean>();
    private long mExitTime;

    private Button btnConnectAndClose = null;
    private int ConnectOrClose = 0;

    private EditText et_ip1 = null;
    private EditText et_ip2 = null;
    private EditText et_ip3 = null;
    private EditText et_ip4 = null;
    private EditText et_port = null;

    private Button btn_pt = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showDialog();
        requestPermission();
        btnConnectAndClose = (Button) findViewById(R.id.btn_wifi_connectandclose);
        Button btn_U = (Button) findViewById(R.id.btn_wifi_U);
        Button btn_FM = (Button) findViewById(R.id.btn_wifi_FM);
        Button btn_MD = (Button) findViewById(R.id.btn_wifi_MD);
        Button btn_print = (Button) findViewById(R.id.btn_next);
        Button btn_rfid_c = (Button) findViewById(R.id.btn_rfid_calibration);
        //Button btn_print_last = (Button) findViewById(R.id.btn_print_lastlabel);
        Button btn_print_300demo = (Button) findViewById(R.id.btn_print_300demo);
        Button btn_print_200demo = (Button) findViewById(R.id.btn_print_200demo);
        Button sentPPLE = (Button) findViewById(R.id.sentPPLE);

        @SuppressLint("HandlerLeak") Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                super.handleMessage(msg);
                if (msg.what == CDFPTKAndroid.PTK_MSG_WHAT_WIFICONNECT) {
                    if (msg.arg1 == 0) {
                        ConnectOrClose = 1;
                        btnConnectAndClose.setText(R.string.disconnect);
                        Toast.makeText(MainActivity.this, R.string.connect_success, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, R.string.disconnect_wifi, Toast.LENGTH_SHORT).show();

                    }
                }
            }
        };
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());

        cdf = new CDFPTKAndroidImpl(this, handler);
        et_ip1 = (EditText) findViewById(R.id.et_wifi_ip1);
        et_ip2 = (EditText) findViewById(R.id.et_wifi_ip2);
        et_ip3 = (EditText) findViewById(R.id.et_wifi_ip3);
        et_ip4 = (EditText) findViewById(R.id.et_wifi_ip4);
        et_port = (EditText) findViewById(R.id.et_wifi_port);

        btnConnectAndClose.setOnClickListener(this);
        btn_U.setOnClickListener(this);
        btn_FM.setOnClickListener(this);
        btn_MD.setOnClickListener(this);
        btn_print.setOnClickListener(this);
        btn_rfid_c.setOnClickListener(this);
        //btn_print_last.setOnClickListener(this);
        btn_print_300demo.setOnClickListener(this);
        btn_print_200demo.setOnClickListener(this);
        sentPPLE.setOnClickListener(this);


        btn_pt = findViewById(R.id.btn_pt);
        btn_pt.setOnClickListener(this);
    }

    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            int chec = checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
            if (chec != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        } else {
            setResult(RESULT_CODE);

        }
    }

    private void showDialog() {

        ImageView view = new ImageView(this);
        CursorDialog dialog = new CursorDialog.Builder(this)
                .full()
                .setView(view)
                .bilder();
        view.setBackgroundResource(R.drawable.postek2);
        dialog.show(2000);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, R.string.exit, Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int i = v.getId();
        if (i == R.id.btn_wifi_connectandclose) {
            if (ConnectOrClose == 0) {
                String ip = et_ip1.getText().toString() + "." + et_ip2.getText().toString() + "." + et_ip3.getText().toString() + "." + et_ip4.getText().toString();
                int port = Integer.parseInt(et_port.getText().toString());
                cdf.PTK_ConnectWiFi(ip, port);
            }
            if (ConnectOrClose == 1) {
                cdf.PTK_CloseConnectWiFi();
                ConnectOrClose = 0;
                btnConnectAndClose.setText(R.string.connect);
                Toast.makeText(MainActivity.this, R.string.unConnect, Toast.LENGTH_SHORT).show();
            }
        } else if (i == R.id.btn_wifi_U) {
            if (ConnectOrClose == 0) {
                Toast.makeText(MainActivity.this, R.string.connect_wifi, Toast.LENGTH_SHORT).show();
            } else
                cdf.PTK_PrintConfiguration();
        } else if (i == R.id.btn_wifi_FM) {
            if (ConnectOrClose == 0) {
                Toast.makeText(MainActivity.this, R.string.connect_wifi, Toast.LENGTH_SHORT).show();
            } else
                cdf.PTK_FeedMedia();
        } else if (i == R.id.btn_wifi_MD) {
            if (ConnectOrClose == 0) {
                Toast.makeText(MainActivity.this, R.string.connect_wifi, Toast.LENGTH_SHORT).show();
            } else
                cdf.PTK_MediaDetect();
        } else if (i == R.id.btn_next) {
            if (ConnectOrClose == 0) {
                Toast.makeText(MainActivity.this, R.string.connect_wifi, Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, PrintActivity.class);
                this.startActivity(intent);
            }
        } else if (i == R.id.sentPPLE) {
            if (ConnectOrClose == 0) {
                Toast.makeText(MainActivity.this, R.string.connect_wifi, Toast.LENGTH_SHORT).show();
            } else
                startActivity(new Intent(MainActivity.this, SentPPLE.class));
        } else if (i == R.id.btn_rfid_calibration) {
            if (ConnectOrClose == 0) {
                Toast.makeText(MainActivity.this, R.string.connect_wifi, Toast.LENGTH_SHORT).show();
            } else {
                int nReturn = cdf.PTK_RFIDCalibration();
                if (nReturn == 0) {
                    Toast.makeText(this, R.string.calibration_success, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, R.string.calibration_fail, Toast.LENGTH_SHORT).show();
                }
            }
            //		case R.id.btn_print_lastlabel:
//			if(ConnectOrClose==0){
//				Toast.makeText(MainActivity.this,R.string.connect_wifi,Toast.LENGTH_SHORT).show();
//			}else
//			printLastLabel();
//			break;
        } else if (i == R.id.btn_print_300demo) {
            if (ConnectOrClose == 0) {
                Toast.makeText(MainActivity.this, R.string.connect_wifi, Toast.LENGTH_SHORT).show();
            } else
                cdf.PTK_Print300DpiDemo();
        } else if (i == R.id.btn_print_200demo) {
            if (ConnectOrClose == 0) {
                Toast.makeText(MainActivity.this, R.string.connect_wifi, Toast.LENGTH_SHORT).show();
            }
//            else
//                cdf.PTK_Print200DpiDemo();
        }

        //
        else if (i == R.id.btn_pt) {
            if(ConnectOrClose==1){
                cdf.PTK_ClearBuffer();
                cdf.PTK_SetPrintSpeed(4);
                cdf.PTK_SetDarkness(10);
                cdf.PTK_SetDirection("T");
                cdf.PTK_SetLabelWidth(832);
                cdf.PTK_SetLabelHeight(600, 16, 0, false);


                //二维码打印的时候 必须以"""做起始
//        printStrs[0] = "\"" + showMsg + "\"";

                String showMsg = "\"test1\"";

                int code = cdf.PTK_DrawBar2D_QR(320, 220, 100, 100, 0, 5, 0, 0, 0, showMsg);

                if (code == 0) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "打印成功", Toast.LENGTH_LONG).show();
                        }
                    });
                } else {
//                    Toast.makeText(this, "打印失败，code = " + code, Toast.LENGTH_LONG).show();
                }
            }
        }

    }

    public void printLastLabel() {
        if (labelBeans.size() == 0) {
            Toast.makeText(this, R.string.no_context, Toast.LENGTH_SHORT).show();
        } else {
            cdf.PTK_ClearBuffer();
            cdf.PTK_SetPrintSpeed(messageBean.getiS());
            cdf.PTK_SetDarkness(messageBean.getiH());
            cdf.PTK_SetDirection(messageBean.getiZ());
            cdf.PTK_SetLabelWidth(messageBean.getiq());
            cdf.PTK_SetLabelHeight(messageBean.getiQ1(),
                    messageBean.getiQ2(), messageBean.getiQ3(),
                    messageBean.getiQ4());

            LabelBean labelBean = new LabelBean();

            for (int i = 0; i < labelBeans.size(); i++) {
                labelBean = labelBeans.get(i);
                if (labelBean.getType().equals("T")) {
                    TBean bean = (TBean) labelBean;
                    cdf.PTK_DrawText(bean.getiT1(), bean.getiT2(),
                            bean.getiT3(), bean.getiT4(), bean.getiT5(),
                            bean.getiT6(), bean.getiT7(), bean.getiTS());
                } else if (labelBean.getType().equals("B")) {
                    BBean bean = (BBean) labelBean;
                    cdf.PTK_DrawBarcode(bean.getiB1(), bean.getiB2(),
                            bean.getiB3(), bean.getiB4(), bean.getiB5(),
                            bean.getiB6(), bean.getiB7(), bean.getiB8(),
                            bean.getiBS());
                } else if (labelBean.getType().equals("DM")) {
                    DMBean bean = (DMBean) labelBean;
                    cdf.PTK_DrawBar2D_DATAMATRIX(bean.getiDM1(),
                            bean.getiDM2(), bean.getiDM3(), bean.getiDM4(),
                            bean.getiDM5(), bean.getiDM6(), bean.getiDMS());
                } else if (labelBean.getType().equals("HX")) {
                    HXBean bean = (HXBean) labelBean;
                    cdf.PTK_DrawBar2D_HANXIN(bean.getiHX1(),
                            bean.getiHX2(), bean.getiHX3(), bean.getiHX4(),
                            bean.getiHX5(), bean.getiHX6(), bean.getiHX7(),
                            bean.getiHX8(), bean.getiHX9(), bean.getiHXS());
                } else if (labelBean.getType().equals("MX")) {
                    MXBean bean = (MXBean) labelBean;
                    cdf.PTK_DrawBar2D_MaxiCode(bean.getiMX1(),
                            bean.getiMX2(), bean.getiMX3(), bean.getiMX4(),
                            bean.getiMXS());
                } else if (labelBean.getType().equals("PDF")) {
                    PDFBean bean = (PDFBean) labelBean;
                    cdf.PTK_DrawBar2D_Pdf417(bean.getiPDF1(),
                            bean.getiPDF2(), 0, 0, bean.getiPDF3(),
                            bean.getiPDF4(), bean.getiPDF5(),
                            bean.getiPDF6(), bean.getiPDF7(),
                            bean.getiPDF8(), bean.getiPDF9(),
                            bean.getiPDF10(), bean.getiPDFS());
                } else if (labelBean.getType().equals("QR")) {
                    QRBean bean = (QRBean) labelBean;
                    cdf.PTK_DrawBar2D_QR(bean.getiQR1(), bean.getiQR2(),
                            bean.getiQR3(), bean.getiQR4(), bean.getiQR5(),
                            bean.getiQR6(), bean.getiQR7(), bean.getiQR8(),
                            bean.getiQR9(), bean.getiQRS());
                } else if (labelBean.getType().equals("LE")) {
                    LEBean bean = (LEBean) labelBean;
                    cdf.PTK_DrawLineXor(bean.getiLE1(), bean.getiLE2(),
                            bean.getiLE3(), bean.getiLE4());
                } else if (labelBean.getType().equals("LE")) {
                    LOBean bean = (LOBean) labelBean;
                    cdf.PTK_DrawLineOr(bean.getiLO1(), bean.getiLO2(),
                            bean.getiLO3(), bean.getiLO4());
                } else if (labelBean.getType().equals("LW")) {
                    LWBean bean = (LWBean) labelBean;
                    cdf.PTK_DrawWhiteLine(bean.getiLW1(), bean.getiLW2(),
                            bean.getiLW3(), bean.getiLW4());
                } else if (labelBean.getType().equals("LS")) {
                    LSBean bean = (LSBean) labelBean;
                    cdf.PTK_DrawDiagonal(bean.getiLS1(), bean.getiLS2(),
                            bean.getiLS3(), bean.getiLS4(), bean.getiLS5());
                } else if (labelBean.getType().equals("X")) {
                    XBean bean = (XBean) labelBean;
                    cdf.PTK_DrawRectangle(bean.getiX1(), bean.getiX2(),
                            bean.getiX3(), bean.getiX4(), bean.getiX5());
                }
            }

            cdf.PTK_PrintLabel(1, 1);
        }
    }
}
